package jmind.core.jdbc;

import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

/**
 * db 类，每一个database 继承这个类
 * @author weibo.xie
 * 2011-10-21
 */
public abstract class TDatabase extends TJdbc {

    private String tableName;
    private static DatabaseConfig config;

    public TDatabase() {
        tableName = this.getClass().getSimpleName();
    }

    @Override
    public JdbcTemplate getJdbc(int source) {
        // TODO Auto-generated method stub
        String name = config.getDatabase(source);
        return (JdbcTemplate) JdbcFactory.getFactory().getBean(name);
    }



    public static void init(String name) {
        config = new DatabaseConfig(name);
    }

    /**
     * 取简单sql
     * @param key
     * @return
     */
    public String getSql(String key) {
        return config.getSql(tableName, key);
    }

    /**
     * 修改操作

     * @param args
     * @return
     */
    public int update(String key, Object... args) {
        return super.update(getSql(key), args);
    }

    public <T> T findOne(int source, String key, Object... args) {
        return super.findOne(source, getSql(key), args);
    }

    public <T> List<T> find(int source, String key, Object... args) {
        return super.find(source, getSql(key), args);
    }

    public <T> T findColumValue(int source, String key, Class<T> requiredType, Object... args) {
        return super.findColumValue(source, getSql(key), requiredType, args);
    }

    public int findForInt(int source, String key, Object... args) {
        return super.findForInt(source, getSql(key), args);
    }

    /**
     * 查找一条记录

     * @param args
     * @return
     */
    public Map<String, Object> findForMap(int source, String key, Object... args) {
        return super.findForMap(source, getSql(key), args);

    }

    /**
     * 查找多条记录

     * @param args
     * @return
     */
    public List<Map<String, Object>> findForList(int source, String key, Object... args) {
        return super.findForList(source, getSql(key), args);
    }

}
